<template>
  <div>
    <!-- 列表组件 -->
    <el-table :data="userlist" border style="width: 100%">
      <el-table-column prop="uid" label="用户编号" width="180">
      </el-table-column>
      <el-table-column prop="username" label="用户名称" width="180">
      </el-table-column>
      <el-table-column prop="rolename" label="所属角色"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            <span v-if="item.row.status == 1">正常</span>
            <span v-else>禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button class="btnInfo" type="primary" size="mini" @click="edit(item.row.uid)">编辑</el-button>
            <el-button class="btnInfo" type="danger" size="mini" @click="del(item.row.uid)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件
    background 是否为分页按钮添加背景色
    layout	组件布局，子组件名用逗号分隔
    total	总条目数
    page-size	每页显示条目个数，支持 .sync 修饰符
    EVENTS
    current-change	currentPage 改变时会触发 
    -->
    <el-pagination @current-change="changePage" background :page-size="pageSize" layout="prev, pager, next"
      :total="count">
    </el-pagination>
  </div>
</template>

<script>
//引入辅助性函数
import { mapGetters, mapActions } from "vuex";
//引入你封装好的接口
import { userdelete } from "../../request/api";
export default {
  computed: {
    ...mapGetters({
      userlist: "user/getUser",
      pageSize: 'user/getSize',
      count: 'user/getCount'
    }),
  },
  mounted() {
    this.getUserAction();
    this.getCount()
  },
  methods: {
    ...mapActions({
      getUserAction: "user/getUserAction",
      getCount: 'user/getCountAction',
      changePageAction: 'user/changePageAction'
    }),
    //封装一个切换页面的事件
    changePage(n) {
      this.changePageAction(n)
    },
    //点击编辑按钮事件
    edit(id) {
      this.$emit("edit", id);
    },
    //封装一个删除事件
    del(uid) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          userdelete({
            uid,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //重新调用列表接口
              this.getUserAction()
              //重新调用总数
              this.getCount()
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>

</style>
